<?php
class Category_model extends MY_Model
{
	public
		$table = 'categories',
		$prKey = 'id'
	;

	function __construct()
	{
		parent::__construct();

		$this->fields = array(
			'id' 		=> '',
			'name' 		=> T_('Name'),
			'description'=> T_('Content'),
		);
			
		$this->rules = array(
			'id' 		=> "trim|max_length[255]",
			'name' 		=> "trim|required|max_length[255]",
			'description' => "trim",
		);
	}
	
	function search($config = array())
	{
		$order 		= $this->input->get_post('order');
		$direction 	= $this->input->get_post('direction');
		$search 	= $this->input->get_post('search');
		
		// main sql		
		$sql = '';
		$sql .= ' FROM '.$this->table.' c';
		$sql .= ' WHERE 1 = 1'; 
								
		if ($search)
		{
			$sql .= ' AND c.name LIKE ' . $this->db->escape('%' . $search . '%'); 
		}
		
		switch ($order)
		{			
			default:
				$sql .= ' ORDER by c.id';
				$direction = 'DESC';
		}
		
		$sql .= ($direction == 'DESC') ? ' DESC' : ' ASC';
		
		// main sql
		$sql = 'SELECT *' . $sql; 

		$this->data['list'] = $this->db->query($sql)->result();
		$this->data['search'] = $search;
	}
	
	function get($id = null)
	{		
		$this->db->from($this->table);
		$this->db->where("id", $id);
		$query = $this->db->get();
		
		return $query->row_array();
	}		
}
?>